const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}
const formatTime2 = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

Date.prototype.format = function (fmt) {
  var o = {
      "M+": this.getMonth() + 1, //月份
      "d+": this.getDate(), //日
      "h+": this.getHours(), //小时
      "m+": this.getMinutes(), //分
      "s+": this.getSeconds(), //秒
      "q+": Math.floor((this.getMonth() + 3) / 3), //季度
      "S": this.getMilliseconds() //毫秒
  };
  if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  for (var k in o)
      if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  return fmt;
}

const formateDate = date=>{
  return /\d{4}-\d{1,2}-\d{1,2}/g.exec(date)
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

const regPhone = phone => {
  var phonetel = /^[1][0-9]{10}$/;
  if (!phonetel.test(phone)) {
    return false
  } else {
    return true
  }
}
const regIdentity = Identity => {
  var regulars = {
    idCard15: /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$/,
    idCard18: /^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$/
  };
  var re1 = new RegExp(regulars.idCard15);
  var re2 = new RegExp(regulars.idCard18);

  if (!re1.test(Identity) && !re2.test(Identity)) {
    return false
  } else {
    return true
  }

}
const isEmailAvailable = (emailInput) => {
  var myreg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
  if (!myreg.test(emailInput)) {
    return false;
  } else {
    return true;
  }
}
const checkTel = (tel) => {
  var isPhone = /^([0-9]{3,4}-)?[0-9]{7,8}$/;


  if (isPhone.test(tel)) {
    return true;
  } else {
    return false;
  }
}
const checkint =(num)=>{
  var isint = /(^[1-9]\d*$)/;

  if (isint.test(num)) {
    return true;
  } else {
    return false;
  }
}

const checknumber = (num)=>{
  var isnumber = /^[0-9]*$/;
  if (isnumber.test(num)) {
    return true;
  } else {
    return false;
  }
}


module.exports = {
  formatTime: formatTime,
  formatTime2:formatTime2,
  formateDate:formateDate,
  regPhone: regPhone,
  isEmail: isEmailAvailable,
  checkTel: checkTel,
  regIdentity: regIdentity,
  checkint:checkint,//非零正整数
  checknumber:checknumber
}